package com.lxmes.hr.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.hr.domain.NcLeaveApprovalRules;
import com.lxmes.hr.service.INcLeaveApprovalRulesService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 请假审批规则Controller
 *
 * @author lx
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/hr/leaveApprova")
public class NcLeaveApprovalRulesController extends BaseController
{
    @Autowired
    private INcLeaveApprovalRulesService ncLeaveApprovalRulesService;

    /**
     * 查询请假审批规则列表
     */
    @PreAuthorize("@ss.hasPermi('hr:leaveApprova:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcLeaveApprovalRules ncLeaveApprovalRules)
    {
        startPage();
        List<NcLeaveApprovalRules> list = ncLeaveApprovalRulesService.selectNcLeaveApprovalRulesList(ncLeaveApprovalRules);
        return getDataTable(list);
    }

    /**
     * 导出请假审批规则列表
     */
    @PreAuthorize("@ss.hasPermi('hr:leaveApprova:export')")
    @Log(title = "请假审批规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcLeaveApprovalRules ncLeaveApprovalRules)
    {
        List<NcLeaveApprovalRules> list = ncLeaveApprovalRulesService.selectNcLeaveApprovalRulesList(ncLeaveApprovalRules);
        ExcelUtil<NcLeaveApprovalRules> util = new ExcelUtil<NcLeaveApprovalRules>(NcLeaveApprovalRules.class);
        util.exportExcel(response, list, "请假审批规则数据");
    }

    /**
     * 获取请假审批规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('hr:leaveApprova:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncLeaveApprovalRulesService.selectNcLeaveApprovalRulesById(id));
    }

    /**
     * 新增请假审批规则
     */
    @PreAuthorize("@ss.hasPermi('hr:leaveApprova:add')")
    @Log(title = "请假审批规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcLeaveApprovalRules ncLeaveApprovalRules)
    {
        return toAjax(ncLeaveApprovalRulesService.insertNcLeaveApprovalRules(ncLeaveApprovalRules));
    }

    /**
     * 修改请假审批规则
     */
    @PreAuthorize("@ss.hasPermi('hr:leaveApprova:edit')")
    @Log(title = "请假审批规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcLeaveApprovalRules ncLeaveApprovalRules)
    {
        return toAjax(ncLeaveApprovalRulesService.updateNcLeaveApprovalRules(ncLeaveApprovalRules));
    }

    /**
     * 删除请假审批规则
     */
    @PreAuthorize("@ss.hasPermi('hr:leaveApprova:remove')")
    @Log(title = "请假审批规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncLeaveApprovalRulesService.deleteNcLeaveApprovalRulesByIds(ids));
    }

    /**
     * 导入请假审批规则
     */
    @Log(title = "请假审批规则", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcLeaveApprovalRules> util = new ExcelUtil<>(NcLeaveApprovalRules.class);
        List<NcLeaveApprovalRules> ncLeaveApprovalRulesList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncLeaveApprovalRulesService.imporIteminfo(ncLeaveApprovalRulesList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcLeaveApprovalRules> util = new ExcelUtil<>(NcLeaveApprovalRules.class);
        util.importTemplateExcel(response,"请假审批规则");
    }
}
